package com.beyondsoft.users.service.impl;

import com.beyondsoft.common.core.model.Users;
import com.beyondsoft.common.core.model.dto.UsersDTO;
import com.beyondsoft.common.core.model.vo.UsersSearchVO;
import com.beyondsoft.common.core.model.vo.UsersVO;
import com.beyondsoft.common.core.sys.annotations.MsTransactional;
import com.beyondsoft.common.core.sys.exception.GeneralException;
import com.beyondsoft.common.core.util.PageBean;
import com.beyondsoft.users.dao.UsersMapper;
import com.beyondsoft.users.service.IUsersService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Random;

/**
 * @author codeBuilder
 * @Description Users业务接口
 * @date 2019-6-25 15:34:21
 */
@Transactional
@Service
public class UsersServiceImpl implements IUsersService {

    @Autowired
    private UsersMapper usersMapper;

    /**
     * @param usersSearchVO
     * @return PageBean<Users>
     * @Description 分页查询Users
     * @date 2019-6-25 15:34:21
     * @author codeBuilder
     */
    public PageBean<UsersDTO> page(UsersSearchVO usersSearchVO) {
        PageHelper.startPage(usersSearchVO.getCurrentPage(), usersSearchVO.getPageSize());
        List<UsersDTO> list = usersMapper.page(usersSearchVO);
        // 返回对象信息
        PageBean<UsersDTO> pagebean = new PageBean(new PageInfo(list));
        return pagebean;
    }

    /**
     * @param users
     * @return List<T>
     * @Description 根据条件查询Users 返回一个list集合
     * @date 2019-6-25 15:34:21
     * @author codeBuilder
     */
    public List<Users> find(Users users) {
        return usersMapper.find(users);
    }

    /**
     * @param id
     * @return Map<K, V>
     * @Description 根据Id查询Users
     * @date 2019-6-25 15:34:21
     * @author codeBuilder
     */
    public UsersDTO findOne(Long id) {
        return usersMapper.findOne(id);
    }

    /**
     * @param usersVO
     * @Description 添加Users
     * @date 2019-6-25 15:34:21
     * @author codeBuilder
     */
    @MsTransactional
    public void insert(UsersVO usersVO) {
        //用VO接收 处理其他逻辑
        usersVO.setUserName("test");
        usersMapper.insert(usersVO);
//        //测试超时
//        try {
//            Thread.sleep(11000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        //随机失败 测试分布式事务
        Random random = new Random();
        if (random.nextInt() % 2 == 0) {
            throw new GeneralException("users 执行失败");
        }
    }

    /**
     * @param usersVO
     * @Description 修改Users
     * @date 2019-6-25 15:34:21
     * @author codeBuilder
     */
    @MsTransactional
    public void update(UsersVO usersVO) {
        //用VO接收 处理其他逻辑
        usersMapper.update(usersVO);
    }

    /**
     * @param id
     * @Description 删除Users
     * @date 2019-6-25 15:34:21
     * @author codeBuilder
     */
    public void delete(Long id) {
        usersMapper.delete(id);
    }

}


